package com.eduction.modules.system.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.eduction.common.dbutil.DbHelper;
import com.eduction.modules.system.entity.CourseAnalyse;

@Repository
public class CourseAnalyseRepository {

	@Autowired
	private DbHelper dbhelper;
	
	@Autowired
	private CourseAnalyseDao courseAnalyseDao;
	
	/**
	 * 保存表格信息
	 */
	public String saveCourseAnalyse(CourseAnalyse c) throws SQLException{
		this.courseAnalyseDao.save(c);
		return c.getPid();
	}
	
	/**
	 * 查看某个表格信息
	 */
	public CourseAnalyse queryCourseAnalyseById(String id,String eid,String sid) throws SQLException{
		String sql = "select a.*,s.s_name as schoolName,e.s_name as expertName from t_course_analyse as a left join t_school as s "
				+" on a.school_id = s.p_id left join t_expert as e on a.expert_id = e.p_id where a.sign = ? and a.expert_id =? and a.school_id = ?";
		return this.dbhelper.getBean(sql, CourseAnalyse.class, id,eid,sid);
	}

	/**
	 * 查看某个表格信息
	 */
	public CourseAnalyse queryCourseAnalyseById(String id,String sid) throws SQLException{
		String sql = "select a.*,s.s_name as schoolName,e.s_name as expertName from t_course_analyse as a left join t_school as s "
				+" on a.school_id = s.p_id left join t_expert as e on a.expert_id = e.p_id where a.sign = ? and a.school_id = ?";
		return this.dbhelper.getBean(sql, CourseAnalyse.class, id,sid);
	}
	
	/**
	 * 查询这个专家创建的所有表格信息
	 * @throws SQLException 
	 */
	public List<CourseAnalyse> queryCAsByExpert(String expertId) throws SQLException{
		String sql = "SELECT a.p_id as pid,s.s_name as schoolName,a.school_id as schoolId,a.sign as sign from t_course_analyse as a LEFT JOIN t_school as s on a.school_id = s.p_id where a.expert_id = ?";
		return this.dbhelper.getBeanList(sql, CourseAnalyse.class, expertId);
	}

    public CourseAnalyse findById(String pId) {
		return courseAnalyseDao.findOne(pId);
    }

    public void deleteCourseAnalyseMapState(String sign, String schoolId, String expertId)  throws SQLException{
		String sql = "delete from t_course_analyse where sign =? and school_id=? and expert_id=? ";
		this.dbhelper.execute(sql,sign,schoolId,expertId);
    }
}
